﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

using DICE_GS;


//이후에 게임 이기고 자동으로 스킬을 다시 받아오는 로직이 추가되면 수정이 필요함
//이전 스킬 갯수 케쉬 후 스킬리스트 업뎃해서 현재 스킬 갯수 비교해서 스킬 획득 표시
public class Popup_Levelup : Popup
{
    static string[] text = new string[5]
    {
        "Lv.{0}가 되었습니다!",
        "Lv.{0}",
        "스태미너   +{0}",
        "코스트  +{0}",
        "{0}Lv. 스킬을 사용할 수 있게 되었습니다.\n\n지금 확인하시겠습니까?"
    };

    const int LevelGageMax = 460;

    int step = 0;

    public GameObject m_Step1Root;
    public GameObject m_Step2Root;

    //step 1
    public UILabel m_LevelLabel_Top;
    public UILabel m_LevelLabel_Mid;
    public UILabel[] m_StatLabel;
    public UISprite m_LevelGage;

    //step 2
    public UILabel m_Title;
    public UILabel m_Body;

    int m_PreSkillCount = 0;
    int m_CrrSkillCount = 0;

    static int cashedPreLevel = 0;
    static int cashedCrrLevel = 0;
    static long cashedExp = 0;


    static public void SetCash()
    {
        cashedPreLevel = MyInfo.Instance.userLevel;
        cashedCrrLevel = MyInfo.Instance.nextUserLevel;
        cashedExp = MyInfo.Instance.Exp;
    }


    public override void AwakeInit()
    {
        if (CPlayerLevelBonusDataManager.Instance.GetMaxLevel() >= cashedCrrLevel)
        {
            CPlayerLevelBonusData preData = CPlayerLevelBonusDataManager.Instance.GetPlayerLevelBonusData(cashedPreLevel);
            CPlayerLevelBonusData crrData = CPlayerLevelBonusDataManager.Instance.GetPlayerLevelBonusData(cashedCrrLevel);

            int cost = 0;
            int stamina = 0;

            if (preData != null && crrData != null)
            {
                cost = crrData.max_cost - preData.max_cost;
                stamina = crrData.max_stamina - preData.max_stamina;
            }

            m_Step1Root.SetActive(true);
            m_Step2Root.SetActive(false);

            m_LevelLabel_Top.text = string.Format(text[0], cashedCrrLevel);
            m_LevelLabel_Mid.text = string.Format(text[1], cashedCrrLevel);

            int line = 0;

            if (stamina > 0)
            {
                m_StatLabel[line].text = string.Format(text[2], stamina);
                line++;
            }

            if (cost > 0)
            {
                m_StatLabel[line].text = string.Format(text[3], cost);
                line++;
            }

            for (int i = line; i < m_StatLabel.Length; i++)
            {
                m_StatLabel[i].gameObject.SetActive(false);
            }


            int preExp = CUnitExpDataManager.Instance.GetPlayerCurrentLevelMaxExp(cashedCrrLevel - 1);
            int crrExp = CUnitExpDataManager.Instance.GetPlayerCurrentLevelMaxExp(cashedCrrLevel);
            long range = crrExp - preExp;
            long userExp = cashedExp - CUnitExpDataManager.Instance.GetPlayerCurrentLevelMaxExp(0) - preExp;
            double expRate = userExp / (double)range;

            m_LevelGage.width = (int)(LevelGageMax * expRate);

            if (m_LevelGage.width < 0)
                m_LevelGage.width = 0;

            if (m_LevelGage.width > LevelGageMax)
                m_LevelGage.width = LevelGageMax;
        }
    }


    public override void OnOkFunc(GameObject obj)
    {
        if (step == 0)
        {
            isNeedClose = false;
            GoNextStep();
        }
        else
        {
            PopupManager.PopupPop(gameObject);
        }
    }


    public override void OnXFunc(GameObject obj)
    {
        if (step == 0)
        {
            isNeedClose = false;
            GoNextStep();
        }
        else
        {
            PopupManager.PopupPop(gameObject);
        }
    }

    void GoNextStep()
    {
        step++;

        m_PreSkillCount = MyInfo.Instance.MySkillList.Count;

        RestAPI.singleton.GetMySkillList(RestCallback);
    }

    public void RestCallback(Hashtable returnData)
    {
        m_CrrSkillCount = MyInfo.Instance.MySkillList.Count;

        if (m_CrrSkillCount != m_PreSkillCount)      // 새로얻은 스킬이 있을때
        {
            m_Body.text = string.Format(text[4], cashedCrrLevel);

            m_Step1Root.SetActive(false);
            m_Step2Root.SetActive(true);
        }
        else
        {
            PopupManager.PopupPop(gameObject);
        }
    }
}
